package com.pku.control;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.gyf.immersionbar.ImmersionBar;

import net.lucode.hackware.magicindicator.FragmentContainerHelper;
import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

public class ControlActivity extends AppCompatActivity {
    private MagicIndicator magicIndicator;
    private CommonNavigator mCommonNavigator7;
    private List<String> titles;
    LinearLayout titleContainer;
    private ViewPager viewPager;
    private FragmentUpdateAdapter fragmentAdapter;
    private List<Fragment> fragmentList = new ArrayList<>();
    private String maccode;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_control);
        ImmersionBar.with(this).init();
        magicIndicator=findViewById(R.id.magic_indicator);
        viewPager=findViewById(R.id.viewPager);
        Intent intent=getIntent();
        maccode= intent.getStringExtra("maccode");
        Log.d("ssss",maccode);
        initMagicIndicator();
    }

    private void initMagicIndicator() {
        try {
            titles=new ArrayList<>();
            titles.add("CPU");
            titles.add("内存");
            titles.add("磁盘");
            titles.add("网络");
            FragmentContainerHelper mFragmentContainerHelper = new FragmentContainerHelper();
            mCommonNavigator7 = new CommonNavigator(ControlActivity.this);
            mCommonNavigator7.setScrollPivotX(0.65f);
            mCommonNavigator7.setLeftPadding(30);
            mCommonNavigator7.setAdjustMode(true);
            mCommonNavigator7.setAdapter(new CommonNavigatorAdapter() {
                @Override
                public int getCount() {
                    return titles == null ? 0 : titles.size();
                }

                @Override
                public IPagerTitleView getTitleView(Context context, final int index) {
                    SimplePagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                    simplePagerTitleView.setText(titles.get(index));
                    simplePagerTitleView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
                    // 默认字体颜色，未选中字体颜色 9e9e9e
                    simplePagerTitleView.setNormalColor(ContextCompat.getColor(ControlActivity.this, R.color.white));
                    // 选中/字体select颜色 00c853
                    simplePagerTitleView.setSelectedColor(ContextCompat.getColor(ControlActivity.this, R.color.white));
                    simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            viewPager.setCurrentItem(index);
                            mFragmentContainerHelper.handlePageSelected(index);

                        }
                    });
                    return simplePagerTitleView;
                }

                @Override
                public IPagerIndicator getIndicator(Context context) {
                    LinePagerIndicator indicator = new LinePagerIndicator(context);
                    indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                    // 横线距离底部的距离
                    indicator.setYOffset(UIUtil.dip2px(context, 3));
                    indicator.setLineHeight(UIUtil.dip2px(context, 3));
                    indicator.setLineWidth(UIUtil.dip2px(context, 20));
                    indicator.setRoundRadius(UIUtil.dip2px(context, 3));
                    indicator.setStartInterpolator(new AccelerateInterpolator());
                    indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                    // 横线颜色 00c853
                    indicator.setColors(ContextCompat.getColor(ControlActivity.this, R.color.white));
                    return indicator;
                }
            });
            magicIndicator.setNavigator(mCommonNavigator7);
            mFragmentContainerHelper.attachMagicIndicator(magicIndicator);
                titleContainer = mCommonNavigator7.getTitleContainer();// must after setNavigator
                titleContainer.setShowDividers(LinearLayout.SHOW_DIVIDER_MIDDLE);
                titleContainer.setDividerDrawable(new ColorDrawable() {
                    @Override
                    public int getIntrinsicWidth() {
                        return UIUtil.dip2px(ControlActivity.this, 15);
                    }
                });

            CpuFragment CpuFragment=new CpuFragment(maccode);
            MemoryFragment memoryFragment=new MemoryFragment(maccode);
            DiskFragment diskFragment=new DiskFragment(maccode);
            NetFragment netFragment=new NetFragment(maccode);
            fragmentList.add(CpuFragment);
            fragmentList.add(memoryFragment);
            fragmentList.add(diskFragment);
            fragmentList.add(netFragment);
            fragmentAdapter = new FragmentUpdateAdapter(getSupportFragmentManager(), fragmentList, titles);
            viewPager.setAdapter(fragmentAdapter);
            viewPager.setOffscreenPageLimit(fragmentList.size());
            viewPager.setCurrentItem(0);
            viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
                }

                @Override
                public void onPageSelected(int position) {
                    magicIndicator.onPageSelected(position);
                    viewPager.setCurrentItem(position);
                    //selectFilter(position);
                }

                @Override
                public void onPageScrollStateChanged(int state) {
                    magicIndicator.onPageScrollStateChanged(state);
                }
            });
        }catch (Exception e){

        }
    }
}
